<?php
/**
 * Medical Study Design - Grid
 * This handle add/delete followup items in the 
 * medical study
 * 
 * 
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy
 * @license #license#
 * 
 */

use Nawa\MedicalStudy;
use Nawa\MedicalStudy\Followup;
use Fox\Html\Block;
$xview = Xview::instance();
$msid = $xview->val('msid');

$bCustomizing = new Block('bCustomizing',Msg(Nawa\Messages::BUNDLE,Nawa\Messages::LABEL_CUSTOMIZING));
$bCustomizing->collapsed(false);
// Followup Grid
/* @var Fox\Grid */
$grid = $xview->component('medicalstudies','followup',array('msid' => $msid));
$bCustomizing->add($grid);
if($xview->ajax()) {
	// We need to reload data from the DB
	$ms = $xview->component('medicalstudies','object',array('msid' => $msid));
	// Handle Changes
	$itemCounter = array();
	$existingFollowup = array();
	foreach($ms->followup() as $item) {
		/* @var $item \Nawa\MedicalStudy\Followup */
		if(!empty($itemCounter[$item->followupid])) {
			$itemCounter[$item->followupid]++;
		} else {
			$itemCounter[$item->followupid] = 1;
		}
		$existingFollowup[$item->itemid] = $item->getInLineId();
	}
	
	$codes = $xview->val('codes'); 
	if(!empty($codes) && is_array($codes)) {
		$language = $xview->language();
		foreach($codes as $followupid) {
			try {
				
				// Find Followup Item
				$followupItem = Fox\Dao::load(Followup\Item::className(),$followupid.':'.$language);
				$code = null;
				if($followupItem->type == Followup\Item::TYPE_MULTIPLE) {
					if(!empty($itemCounter[$followupid])) {
						$itemCounter[$followupid]++;
					} else {
						$itemCounter[$followupid] = 1;
					}
					
					$code = $followupItem->mask.$itemCounter[$followupid];

				}
				$followup = new Followup();
				$followup->setFields(array(
					'msid' => $msid,
					'followupid' => $followupid,
					'code' => $code,
					'type' => $followupItem->type
				));
				$ms->addToFollowup($followup);
				$ms->prepareUpdate(MedicalStudy::CHANGE_ADD_FOLLOWUP_ITEM);
				$ms->update();
			} catch(\Fox\Object\Exception\Save $e) {
				//@TODO do something here
			}
		}
	}
	
	// Prepare data
	foreach($ms->followup() as $item) {
		$data[$item->itemid] = $item->getData();
	}
	// Update data with posted grid value, because the user can change the code
	// before saving, and we don't this information to be lost
	$postedData = $grid->get(null,true);
	if(is_array($postedData)) {
		
		foreach($postedData as $rowData) {

			if(empty($rowData['itemid'])) {
				continue;
			}
			$itemid = $rowData['itemid'];
	
			if(isset($data[$itemid])) {
				$data[$itemid]['code'] = $rowData['code'];
			}
			if(isset($existingFollowup[$itemid])) {
				unset($existingFollowup[$itemid]);
			}
		}
	}
	// Removed Delete Followup
	foreach($existingFollowup as $itemid => $followup) {
		unset($data[$itemid]);
		//@TODO add save behavior here ?
	}
	$grid->initialize($data,true);
	echo $grid->display();
} else {
	return $bCustomizing;
}